package com.anjiplus.template.gaea.business.modules.flowable.model.service.impl;

import com.anji.plus.gaea.curd.mapper.GaeaBaseMapper;
import com.anjiplus.template.gaea.business.modules.flowable.model.dao.ActDeModelMapper;
import com.anjiplus.template.gaea.business.modules.flowable.model.dao.entity.ActDeModel;
import com.anjiplus.template.gaea.business.modules.flowable.model.service.ActDeModelService;
import org.flowable.bpmn.model.BpmnModel;
import org.flowable.engine.RepositoryService;
import org.flowable.engine.repository.Deployment;
import org.flowable.ui.modeler.domain.Model;
import org.flowable.ui.modeler.serviceapi.ModelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * (ActDeModel)ServiceImpl
 *
 * @author lirui
 * @since 2021-03-17 15:53:44
 */
@Service
public class ActDeModelServiceImpl implements ActDeModelService {
    @Autowired
    private ActDeModelMapper actDeModelMapper;

    @Autowired
    private ModelService modelService;

    @Autowired
    private RepositoryService repositoryService;

    @Override
    public GaeaBaseMapper<ActDeModel> getMapper() {
        return  actDeModelMapper;
    }


    /**
     * 部署模型
     * @param modelId
     * @return
     */
    @Override
    public Deployment publishModel(String modelId) {
        Model model = modelService.getModel(modelId);
        BpmnModel bpmnModel = modelService.getBpmnModel(model);

        //流程发布
        Deployment deployment = repositoryService.createDeployment()
                .name(model.getName())
                .addBpmnModel(model.getKey() + ".bpmn", bpmnModel).deploy();
        return deployment;
    }


}
